<template>
  <div class="app-container">
    <!-- <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="所属课程" prop="courseId">
        <el-select
          v-model="queryParams.courseId"
          placeholder="请选择课程"
          clearable
        >
          <el-option
            v-for="item in this.course"
            :key="item.courseId"
            :label="item.courseName"
            :value="item.courseId"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="题目名称" prop="topicName">
        <el-input
          v-model="queryParams.topicName"
          placeholder="请输入题目名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select
          v-model="queryParams.status"
          placeholder="请选择状态"
          clearable
        >
          <el-option
            v-for="dict in dict.type.cd_topic_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form> -->

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['topic:topic:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['topic:topic:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['topic:topic:remove']"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['topic:topic:export']"
          >导出</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleSubmit"
          v-hasPermi="['topic:topic:edit']"
          >提交</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="topicList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="50" align="center" />
      <el-table-column
        label="题目名称"
        align="center"
        prop="topicName"
        width="100"
      />
      <el-table-column
        label="指导教师"
        align="center"
        prop="teacherName"
        width="100"
      />
      <el-table-column
        label="所属课程"
        align="center"
        prop="courseName"
        width="100"
      />
      <el-table-column label="题目描述" align="center" prop="topicDescribe" />
      <el-table-column
        label="题目附件"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="small"
            icon="el-icon-download"
            @click="handleDownload(scope.row.fileByTeacher)"
            v-hasPermi="['topic:topic:list']"
            v-if="scope.row.fileByTeacher != null"
            >下载</el-button
          >
          <div v-if="scope.row.fileByTeacher === null">无</div>
        </template>
      </el-table-column>
      <el-table-column
        label="学生上传"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="small"
            icon="el-icon-download"
            @click="handleDownload(scope.row.fileByStudent)"
            v-hasPermi="['topic:topic:list']"
            v-if="scope.row.fileByStudent != null"
            >下载</el-button
          >
          <div v-if="scope.row.fileByStudent === null">无</div>
        </template>
      </el-table-column>
      <el-table-column
        label="开始检查时间"
        align="center"
        prop="checkStartTime"
        width="120"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.checkStartTime, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="结束检查时间"
        align="center"
        prop="checkEndTime"
        width="120"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.checkEndTime, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <!-- <el-table-column
        label="学生预约时间"
        align="center"
        prop="checkAppointTime"
        width="100"
      >
        <template slot-scope="scope">
          <span>{{
            parseTime(scope.row.checkAppointTime, "{y}-{m}-{d}")
          }}</span>
        </template>
      </el-table-column> -->
      <el-table-column
        label="最大人数"
        align="center"
        prop="maxNum"
        width="80"
      />
      <el-table-column label="状态" align="center" prop="status" width="110">
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.cd_topic_status"
            :value="scope.row.status"
          />
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['topic:topic:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleSubmit(scope.row)"
            v-hasPermi="['topic:topic:edit']"
            v-if="scope.row.status === '0'"
            >提交</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handlePass(scope.row)"
            v-hasPermi="['topic:topic:trail']"
            v-if="scope.row.status === '10'"
            >通过</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleFail(scope.row)"
            v-hasPermi="['topic:topic:trail']"
            v-if="scope.row.status === '10'"
            >不通过</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleChooseTopic(scope.row)"
            v-hasPermi="['topic:topic:choose']"
            v-if="scope.row.status === '20'"
            >选题</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleStuUpdate(scope.row)"
            v-hasPermi="['topic:topic:stuSubmit']"
            v-if="scope.row.status > '20'"
            >学生提交材料</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleStart(scope.row)"
            v-hasPermi="['topic:topic:start']"
            v-if="scope.row.status === '20'"
            >开题</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleAppoint(scope.row)"
            v-hasPermi="['topic:topic:appoint']"
            v-if="scope.row.status === '30'"
            >预约</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleAccept(scope.row)"
            v-hasPermi="['topic:topic:edit']"
            v-if="scope.row.status === '40'"
            >同意</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleReject(scope.row)"
            v-hasPermi="['topic:topic:edit']"
            v-if="scope.row.status === '40'"
            >拒绝</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['topic:topic:remove']"
            v-if="scope.row.status === '0'"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改题目信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="所属课程" prop="courseId">
          <el-select v-model="form.courseId" placeholder="请选择课程" clearable>
            <el-option
              v-for="item in this.course"
              :key="item.courseId"
              :label="item.courseName"
              :value="item.courseId"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="题目名称" prop="topicName">
          <el-input v-model="form.topicName" placeholder="请输入题目名称" />
        </el-form-item>
        <el-form-item label="题目描述" prop="topicDescribe">
          <el-input v-model="form.topicDescribe" placeholder="请输入题目描述" />
        </el-form-item>
        <el-form-item label="附件上传" prop="fileByTeacher">
          <file-upload v-model="form.fileByTeacher" />
        </el-form-item>
        <el-form-item label="开始时间" prop="checkStartTime">
          <el-date-picker
            clearable
            v-model="form.checkStartTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择开始检查时间"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="结束时间" prop="checkEndTime">
          <el-date-picker
            clearable
            v-model="form.checkEndTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择结束检查时间"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="最大人数" prop="maxNum">
          <el-input v-model="form.maxNum" placeholder="请输入最大人数" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 学生提交材料 -->
    <el-dialog
      :title="title"
      :visible.sync="StuOpen"
      width="500px"
      append-to-body
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="附件上传" prop="fileByStudent">
          <file-upload v-model="form.fileByStudent" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="handleStuSubmit">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listTopic,
  getTopic,
  delTopic,
  addTopic,
  updateTopic,
  listBySelf,
  submit,
  pass,
  fail,
  chooseTopic,
  start,
  appoint,
  accept,
  reject,
  stuSubmit,
} from "@/api/coursedesign/topic";
import downloadFile from "@/plugins/download";

export default {
  name: "Topic",
  dicts: ["cd_topic_status"],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 题目信息表格数据
      topicList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        teacherId: null,
        courseId: null,
        topicName: null,
        status: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        courseId: [
          { required: true, message: "所属课程id不能为空", trigger: "blur" },
        ],
        topicName: [
          { required: true, message: "题目名称不能为空", trigger: "blur" },
        ],
        topicDescribe: [
          { required: true, message: "题目描述不能为空", trigger: "blur" },
        ],
      },
      // 课程信息
      course: [],
      // 是否显示学生弹出层
      StuOpen: false,
    };
  },
  created() {
    this.getList();
    this.getListBySelf();
  },
  methods: {
    /** 查询题目信息列表 */
    getList() {
      this.loading = true;
      listTopic(this.queryParams).then((response) => {
        this.topicList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.StuOpen = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        topicId: null,
        teacherId: null,
        courseId: null,
        topicName: null,
        topicDescribe: null,
        fileByTeacher: null,
        fileByStudent: null,
        checkStartTime: null,
        checkEndTime: null,
        checkAppointTime: null,
        maxNum: null,
        status: null,
        delFlag: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.topicId);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加题目信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const topicId = row.topicId || this.ids;
      getTopic(topicId).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改题目信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.topicId != null) {
            updateTopic(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addTopic(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const topicIds = row.topicId || this.ids;
      this.$modal
        .confirm('是否确认删除题目信息编号为"' + topicIds + '"的数据项？')
        .then(function () {
          return delTopic(topicIds);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "topic/topic/export",
        {
          ...this.queryParams,
        },
        `topic_${new Date().getTime()}.xlsx`
      );
    },
    /** 查询自己指导的课程*/
    getListBySelf() {
      listBySelf().then((response) => {
        this.course = response.data;
      });
    },
    /** 提交*/
    handleSubmit(row) {
      const topicId = row.topicId || this.ids;
      submit(topicId).then((response) => {
        this.$modal.msgSuccess(response.msg);
      });
    },
    /** 通过 */
    handlePass(row) {
      const topicId = row.topicId || this.ids;
      pass(topicId).then((response) => {
        this.$modal.msgSuccess(response.msg);
      });
    },
    /** 不通过 */
    handleFail(row) {
      const topicId = row.topicId || this.ids;
      fail(topicId).then((response) => {
        this.$modal.msgSuccess(response.msg);
      });
    },
    /** 学生选题 */
    handleChooseTopic(row) {
      const topicId = row.topicId;
      chooseTopic(topicId).then((response) => {
        this.$modal.msgSuccess(response.msg);
      });
    },
    /** 指导教师开题 */
    handleStart(row) {
      const topicId = row.topicId;
      start(topicId).then((response) => {
        this.$modal.msgSuccess(response.msg);
      });
    },
    /** 学生预约 */
    handleAppoint(row) {
      const topicId = row.topicId;
      appoint(topicId).then((response) => {
        this.$modal.msgSuccess(response.msg);
      });
    },
    /** 教师同意 */
    handleAccept(row) {
      const topicId = row.topicId;
      accept(topicId).then((response) => {
        this.$modal.msgSuccess(response.msg);
      });
    },
    /** 教师不同意 */
    handleReject(row) {
      const topicId = row.topicId;
      reject(topicId).then((response) => {
        this.$modal.msgSuccess(response.msg);
      });
    },
    /** 附件下载 */
    handleDownload(filePath) {
      downloadFile.name(filePath, false);
    },
    /** 学生提交材料弹框 */
    handleStuUpdate(row) {
      this.reset();
      const topicId = row.topicId || this.ids;
      getTopic(topicId).then((response) => {
        this.form = response.data;
        this.StuOpen = true;
        this.title = "上传附件";
      });
    },
    /** 学生提交材料 */
    handleStuSubmit(row) {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          stuSubmit(this.form).then((response) => {
            this.$modal.msgSuccess(response.msg);
            this.StuOpen = false;
            this.getList();
          });
        }
      });
    },
  },
};
</script>
